package com.accp.crm.service;


import org.hibernate.SessionFactory;

import com.accp.crm.dao.CommonDAO;
import com.accp.crm.entity.PageBean;
import com.accp.crm.entity.SalChance;

public class ChanceService implements IChanceService {
  
	private CommonDAO<SalChance> chanceDAO;
	
	public void setSessionFactory(SessionFactory sessionFactory){
		chanceDAO=new CommonDAO<SalChance>(sessionFactory, SalChance.class);
	}
	public void add(SalChance chance){
		this.chanceDAO.save(chance);
	}
	
	public void delete(Long id){
		SalChance chance=this.chanceDAO.findById(id);
		this.chanceDAO.delete(chance);
	}
	
	public SalChance findById(Long id){
		return this.chanceDAO.findById(id);
	}
	
	public PageBean findByStatus(int pageNo,String custName,String title,String linkman) {
		String hql="from SalChance s where s.chcStatus=1";
		if (custName!=null){
			hql += "and s.chcCustName like '%"
					+custName+"%' ";
		}
		if (title!=null){
			hql += "and s.chcTitle like '%"
				+title+"%' ";
		}
		if (linkman!=null){
			hql += "and s.chcLinkman like '%"
				+linkman+"%' ";
		}
		return this.chanceDAO.findByPage(hql,pageNo, 10);
	}
	
	public void update(SalChance chance){
		this.chanceDAO.update(chance);
	}
	
	public PageBean findByPlan(int pageNo,String custName,String title,String linkman){
		String hql="from SalChance s where s.chcStatus !=1";
		if (custName!=null){
			hql += "and s.chcCustName like '%"
					+custName+"%' ";
		}
		if (title!=null){
			hql += "and s.chcTitle like '%"
				+title+"%' ";
		}
		if (linkman!=null){
			hql += "and s.chcLinkman like '%"
				+linkman+"%' ";
		}
		return this.chanceDAO.findByPage(hql,pageNo, 10);
	}
}
